<%--
  User: 薛振宇
  Date: 2025/8/20
  Time: 19:51
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- 接收数据为pageItems{pageSize pageCount currentPage List<User>} --%>
<%
    // 保存当前请求参数
    String roleSelect = request.getParameter("roleSelect");
    String username = request.getParameter("username");
    String id = request.getParameter("id");
    String userId = request.getParameter("userId");
    String classId = request.getParameter("classId");
    String courseSearchText = request.getAttribute("courseSearchText") != null ? request.getAttribute("courseSearchText").toString() : "";
    String subjectSearchText = request.getAttribute("subjectSearchText") != null ? request.getAttribute("subjectSearchText").toString() : "";
    String teacherSearchText = request.getAttribute("teacherSearchText") != null ? request.getAttribute("teacherSearchText").toString() : "";
    String searchType = request.getAttribute("searchType") != null ? request.getAttribute("searchType").toString() : "";

    // 添加题库搜索参数
    String courseId = request.getParameter("courseId");
    String questionTypeId = request.getParameter("questionTypeId");
    String searchText = request.getParameter("searchText");
%>
<nav aria-label="Page navigation example" style="margin-left: auto; margin-right: auto;">
    <ul class="pagination justify-content-center">
        <li class="page-item ${page.pageIndex <= 1 ? 'disabled' : ''}">
            <a class="page-link"
               href="<c:url value="${page.servletName}">
               <c:if test="<%=classId != null%>">
                                <c:param name="classId" value="<%=classId%>"/>
                            </c:if>
                            <c:param name="pageIndex" value="${page.pageIndex - 1}"/>
                            <c:if test="<%=userId != null && !userId.isEmpty()%>">
                                <c:param name="userId" value="<%=userId%>"/>
                            </c:if>
                            <c:if test="<%=roleSelect != null && !roleSelect.isEmpty()%>">
                                <c:param name="roleSelect" value="<%=roleSelect%>"/>
                            </c:if>
                            <c:if test="<%=username != null && !username.isEmpty()%>">
                                <c:param name="username" value="<%=username%>"/>
                            </c:if>
                            <c:if test="<%=id != null && !id.isEmpty()%>">
                                <c:param name="id" value="<%=id%>"/>
                            </c:if>
                            <c:if test="<%=courseSearchText != null && !courseSearchText.isEmpty()%>">
                                <c:param name="courseSearchText" value="<%=courseSearchText%>"/>
                            </c:if>
                            <c:if test="<%=subjectSearchText != null && !subjectSearchText.isEmpty()%>">
                                <c:param name="subjectSearchText" value="<%=subjectSearchText%>"/>
                            </c:if>
                            <c:if test="<%=teacherSearchText != null && !teacherSearchText.isEmpty()%>">
                                <c:param name="teacherSearchText" value="<%=teacherSearchText%>"/>
                            </c:if>
                            <c:if test="<%=!searchType.isEmpty()%>">
                                <c:param name="searchType" value="<%=searchType%>"/>
                            </c:if>
                            <%-- 添加题库搜索参数 --%>
                            <c:if test="<%=courseId != null && !courseId.isEmpty()%>">
                                <c:param name="courseId" value="<%=courseId%>"/>
                            </c:if>
                            <c:if test="<%=questionTypeId != null && !questionTypeId.isEmpty()%>">
                                <c:param name="questionTypeId" value="<%=questionTypeId%>"/>
                            </c:if>
                            <c:if test="<%=searchText != null && !searchText.isEmpty()%>">
                                <c:param name="searchText" value="<%=searchText%>"/>
                            </c:if>
                        </c:url>"
               aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>

        <%-- 修复边界显示问题，确保始终显示合适的页码数量 --%>
        <c:set var="startPage" value="${page.pageIndex - 10}"/>
        <c:set var="endPage" value="${page.pageIndex + 10}"/>

        <%-- 确保起始页不小于1 --%>
        <c:if test="${startPage < 1}">
            <c:set var="startPage" value="1"/>
        </c:if>

        <%-- 确保结束页不大于总页数 --%>
        <c:if test="${endPage > page.totalPages}">
            <c:set var="endPage" value="${page.totalPages}"/>
        </c:if>

        <%-- 如果前面页码不足，补充后面的页码 --%>
        <c:if test="${page.pageIndex - startPage < 10 && endPage < page.totalPages}">
            <c:set var="additionalPages" value="${10 - (page.pageIndex - startPage)}"/>
            <c:set var="endPage"
                   value="${endPage + additionalPages > page.totalPages ? page.totalPages : endPage + additionalPages}"/>
        </c:if>

        <%-- 如果后面页码不足，补充前面的页码 --%>
        <c:if test="${endPage - page.pageIndex < 10 && startPage > 1}">
            <c:set var="additionalPages" value="${10 - (endPage - page.pageIndex)}"/>
            <c:set var="startPage" value="${startPage - additionalPages < 1 ? 1 : startPage - additionalPages}"/>
        </c:if>

        <%-- "${page.totalPages}" --%>
        <c:forEach var="i" begin="${startPage}" end="${endPage}">
            <li class="page-item ${page.pageIndex == i ?  'active' : ''}">

                <a class="page-link"
                    <%-- href="<c:url value="/${page.servletName}?pageIndex=${i}&amp;roleId=${roleId}"/>">${i}--%>
                   href="
                        <c:url value="${page.servletName}">
                        <c:if test="<%=classId != null%>">
                                <c:param name="classId" value="<%=classId%>"/>
                            </c:if>
                            <c:param name="pageIndex" value="${i}"/>
                            <c:if test="<%=userId != null && !userId.isEmpty()%>">
                                <c:param name="userId" value="<%=userId%>"/>
                            </c:if>
                            <c:if test="<%=roleSelect != null && !roleSelect.isEmpty()%>">
                                <c:param name="roleSelect" value="<%=roleSelect%>"/>
                            </c:if>
                            <c:if test="<%=username != null && !username.isEmpty()%>">
                                <c:param name="username" value="<%=username%>"/>
                            </c:if>
                            <c:if test="<%=id != null && !id.isEmpty()%>">
                                <c:param name="id" value="<%=id%>"/>
                            </c:if>
                            <c:if test="<%=courseSearchText != null && !courseSearchText.isEmpty()%>">
                                <c:param name="courseSearchText" value="<%=courseSearchText%>"/>
                            </c:if>
                            <c:if test="<%=subjectSearchText != null && !subjectSearchText.isEmpty()%>">
                                <c:param name="subjectSearchText" value="<%=subjectSearchText%>"/>
                            </c:if>
                            <c:if test="<%=teacherSearchText != null && !teacherSearchText.isEmpty()%>">
                                <c:param name="teacherSearchText" value="<%=teacherSearchText%>"/>
                            </c:if>
                            <c:if test="<%=!searchType.isEmpty()%>">
                                <c:param name="searchType" value="<%=searchType%>"/>
                            </c:if>
                            <%-- 添加题库搜索参数 --%>
                            <c:if test="<%=courseId != null && !courseId.isEmpty()%>">
                                <c:param name="courseId" value="<%=courseId%>"/>
                            </c:if>
                            <c:if test="<%=questionTypeId != null && !questionTypeId.isEmpty()%>">
                                <c:param name="questionTypeId" value="<%=questionTypeId%>"/>
                            </c:if>
                            <c:if test="<%=searchText != null && !searchText.isEmpty()%>">
                                <c:param name="searchText" value="<%=searchText%>"/>
                            </c:if>
                        </c:url>">${i}
                </a>
            </li>
        </c:forEach>
        <li class="page-item ${page.pageIndex >= page.totalPages ? 'disabled' : ''}">
            <a class="page-link"
            <%--               href="<c:url value="/${page.servletName}?pageIndex=${page.pageIndex + 1}&amp;roleId=${roleId}"/>"--%>
               href="<c:url value="${page.servletName}">
               <c:if test="<%=classId != null%>">
                                <c:param name="classId" value="<%=classId%>"/>
                            </c:if>
                            <c:param name="pageIndex" value="${page.pageIndex + 1}"/>
                            <c:if test="<%=userId != null && !userId.isEmpty()%>">
                                <c:param name="userId" value="<%=userId%>"/>
                            </c:if>
                            <c:if test="<%=roleSelect != null && !roleSelect.isEmpty()%>">
                                <c:param name="roleSelect" value="<%=roleSelect%>"/>
                            </c:if>
                            <c:if test="<%=username != null && !username.isEmpty()%>">
                                <c:param name="username" value="<%=username%>"/>
                            </c:if>
                            <c:if test="<%=id != null && !id.isEmpty()%>">
                                <c:param name="id" value="<%=id%>"/>
                            </c:if>
                            <c:if test="<%=courseSearchText != null && !courseSearchText.isEmpty()%>">
                                <c:param name="courseSearchText" value="<%=courseSearchText%>"/>
                            </c:if>
                            <c:if test="<%=subjectSearchText != null && !subjectSearchText.isEmpty()%>">
                                <c:param name="subjectSearchText" value="<%=subjectSearchText%>"/>
                            </c:if>
                            <c:if test="<%=teacherSearchText != null && !teacherSearchText.isEmpty()%>">
                                <c:param name="teacherSearchText" value="<%=teacherSearchText%>"/>
                            </c:if>
                            <c:if test="<%=!searchType.isEmpty()%>">
                                <c:param name="searchType" value="<%=searchType%>"/>
                            </c:if>
                            <%-- 添加题库搜索参数 --%>
                            <c:if test="<%=courseId != null && !courseId.isEmpty()%>">
                                <c:param name="courseId" value="<%=courseId%>"/>
                            </c:if>
                            <c:if test="<%=questionTypeId != null && !questionTypeId.isEmpty()%>">
                                <c:param name="questionTypeId" value="<%=questionTypeId%>"/>
                            </c:if>
                            <c:if test="<%=searchText != null && !searchText.isEmpty()%>">
                                <c:param name="searchText" value="<%=searchText%>"/>
                            </c:if>
                        </c:url>"
               aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
    </ul>
</nav>
